#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: CTF Correction (Part 1)                                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 09/39/2014                                             #
# Last Modification: 03/08/2015	                                            #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 37
#
# MANUAL: This script applies CTF correction to the raw image. Even if you don't use this feature, you need to run this script.
#
# PUBLICATION: 2dx - User-friendly image processing for 2D crystals: <A HREF="http://dx.doi.org/10.1016/j.jsb.2006.07.020">J. Struct. Biol. 157 (2007) 64-72</A>
# PUBLICATION: Image processing of 2D crystal images: <A HREF="http://link.springer.com/protocol/10.1007%2F978-1-62703-176-9_10">Methods in Molecular Biology (2012)</A>
#
# DISPLAY: imagename
# DISPLAY: nonmaskimagename
# DISPLAY: imagenumber
# DISPLAY: imagesidelength
# DISPLAY: comment
# DISPLAY: TLTAXIS
# DISPLAY: TLTANG
# DISPLAY: RESMAX
# DISPLAY: defocus
# DISPLAY: phacon
# DISPLAY: magnification
# DISPLAY: stepdigitizer
# DISPLAY: CS
# DISPLAY: KV
# DISPLAY: realang
# DISPLAY: realcell
# DISPLAY: ctfcor_noise
# DISPLAY: ctfcor_imode
# DISPLAY: ctfcor_debug
# DISPLAY: Thread_Number
# DISPLAY: DEFOCUS_TLTAXIS
# DISPLAY: DEFOCUS_TLTANG
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set SYN_Unbending = ""
set PHASEORI_done = ""
#
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set imagenumber = ""
set imagesidelength = ""
set realcell = ""
set magnification = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set realang = ""
set phacon = ""
set RESMIN = ""
set RESMAX = ""
set RADLIM = ""
set generatePeriodogram = ""
set periodogramWindowsize = ""
set periodogramNumsubsteps = ""
set taperBeforeFFT = ""
set CS = ""
set KV = ""
set phacon = ""
set secondlattice = ""
set TLTAXIS = ""
set TLTANG = ""
set TLTAXA = ""
set TAXA = ""
set TANGL = ""
set defocus = ""
set ctfcor_noise = ""
set ctfcor_imode = ""
set ctfcor_debug = ""
set Thread_Number = ""
set DEFOCUS_TLTAXIS = ""
set DEFOCUS_TLTANG = ""
#
#$end_vars
#
set scriptname = 2dx_ctfcor
#
\rm -f LOGS/${scriptname}.results
#
set IS_2DX = yes
source ${proc_2dx}/initialize
#
echo "<<@progress: 5>>"
#
echo "<<@evaluate>>"
#
echo "# IMAGE-IMPORTANT: ${imagename}.mrc <Input Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: "FFTIR/${imagename}_fft.mrc "<FFT of Image>" >> LOGS/${scriptname}.results
#
if ( x${TLTAXIS} == "x-" || x${TLTAXIS} == "x--" ) then
  set TLTAXIS = "0.0"
endif
if ( x${TLTANG} == "x-" || x${TLTANG} == "x--" ) then
  set TLTANG = "0.0"
endif
if ( x${TLTAXA} == "x-" || x${TLTAXA} == "x--" ) then
  set TLTAXA = "0.0"
endif
if ( x${TAXA} == "x-" || x${TAXA} == "x--" ) then
  set TAXA = "0.0"
endif
if ( x${TANGL} == "x-" || x${TANGL} == "x--" ) then
  set TANGL = "0.0"
endif
#
if (${ctfcor_imode}x == "0x" || ${ctfcor_imode}x == 4x || ${ctfcor_imode}x == 5x || ${ctfcor_imode}x == 6x || ${ctfcor_imode}x == 7x ) then
  ${proc_2dx}/linblock "Not applying any CTF correction to ${imagename}.mrc."
  \cp -f ${imagename}.mrc image_ctfcor.mrc
  echo "# IMAGE-IMPORTANT: image_ctfcor.mrc <Output Image CTF corrected>" >> LOGS/${scriptname}.results
else
  #  
  #
  #################################################################################
  ${proc_2dx}/linblock "2dx_ctfcor - to apply CTF correction to the image ${imagename}.mrc"
  #################################################################################  
  #
  set TLTAXIS_local = ${DEFOCUS_TLTAXIS}
  set TLTANG_local = ${DEFOCUS_TLTANG}
  echo "::Using DEFOCUS TLTAXIS of ${TLTAXIS_local}"
  echo "::Using DEFOCUS TLTANG  of ${TLTANG_local}"    
  #
  if ( ${ctfcor_imode}x == 8x ) then
    set ctfcor_imode_local = 2
  else
    set ctfcor_imode_local = ${ctfcor_imode}
  endif
  #
  \rm -f image_ctfcor.mrc
  #
  setenv NCPUS ${Thread_Number}
  #
  set ctfcor_ctffile = "SCRATCH/2dx_ctfcor_ctffile.mrc"
  \rm -f ${ctfcor_ctffile}  
  #
  ${bin_2dx}/2dx_ctfcor_stripes.exe << eot
${imagename}.mrc
image_ctfcor.mrc
${ctfcor_ctffile}
${TLTAXIS},${TLTANG}
${CS},${KV},${phacon},${magnification},${stepdigitizer}
${defocus}
${RESMAX}
${ctfcor_noise}
${ctfcor_imode_local}
${ctfcor_debug}
eot
  #
  echo "# IMAGE-IMPORTANT: image_ctfcor.mrc <Output Image CTF corrected>" >> LOGS/${scriptname}.results
  if ( ${ctfcor_imode_local} == "2" ) then
    echo "# IMAGE: ${ctfcor_ctffile} <Summed CTF**2 file>" >> LOGS/${scriptname}.results
  else
    echo "# IMAGE: ${ctfcor_ctffile} <Summed CTF file>" >> LOGS/${scriptname}.results
  endif
  #
endif
#
echo "<<@progress: 10>>"
#
#################################################################################
${proc_2dx}/linblock "Sourcing 2dx_fftrans_sub.com"
#################################################################################
#
set inimage = image_ctfcor
source ${proc_2dx}/2dx_fftrans_sub.com
#
#################################################################################
${proc_2dx}/linblock "Back in 2dx_fftrans.com"
#################################################################################
#
#
echo "<<@progress: 100>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
